#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
#include<unordered_map>
#include<vector>
using namespace std;

class Solution {
public:
    int longestArithSeqLength(vector<int>& nums) {
        int n = nums.size();
        vector<vector<int>> dp(n, vector<int>(n, 2));
        unordered_map<int, int> hash;
        int ret = 0;
        for (int i = 0; i < n; ++i) {
            for (int j = i + 1; j < n; ++j) {
                int a = 2 * nums[i] - nums[j];
                if (hash.count(a)) dp[i][j] = dp[hash[a]][i] + 1;
                ret = max(dp[i][j], ret);
            }
            hash[nums[i]] = i;
        }
        return ret;
    }

    int minCut(string s) {
        int n = s.size();
        vector<vector<bool>> f(n, vector<bool>(n));
        vector<int> dp(n, INT_MAX);
        for (int i = 0; i < n; ++i) {
            for (int j = 0; j <= i; ++j) {
                if (s[i] == s[j]) f[j][i] = j + 1 < i ? f[j + 1][i - 1] : true;
            }
        }
        for (int i = 0; i < n; ++i) {
            if (f[0][i])dp[i] = 0;
            else {
                for (int j = 1; j <= i; ++j) {
                    if (f[j][i]) dp[i] = min(dp[j - 1] + 1, dp[i]);
                }
            }
        }
        return dp[n - 1];
    }

    int longestPalindromeSubseq(string s) {
        int n = s.size();
        vector<vector<int>> dp(n, vector<int>(n));
        for (int i = n - 1; i >= 0; --i) {
            for (int j = i; j < n; ++j) {
                if (s[i] == s[j]) {
                    if (i == j) dp[i][j] = 1;
                    else if (i + 1 == j) dp[i][j] = 2;
                    else dp[i][j] = dp[i + 1][j - 1] + 2;
                }
                else {
                    dp[i][j] = max(dp[i + 1][j], dp[i][j - 1]);
                }
            }
        }
        return dp[0][n - 1];
    }

    int minInsertions(string s) {
        int n = s.size();
        vector<vector<int>> dp(n, vector<int>(n));
        for (int i = n - 1; i >= 0; --i) {
            for (int j = i + 1; j < n; ++j) {
                if (s[i] == s[j]) dp[i][j] = dp[i + 1][j - 1];
                else dp[i][j] = min(dp[i + 1][j], dp[i][j - 1]) + 1;
            }
        }
        return dp[0][n - 1];
    }

    int palindromePartition(string s, int k) {
        int n = s.size();
        vector<vector<int>> costs(n, vector<int>(n));
        for (int i = n - 1; i >= 0; --i) {
            for (int j = i; j < n; ++j) {
                if (s[i] == s[j]) {
                    costs[i][j] = i + 1 < j ? costs[i + 1][j - 1] : 0;
                }
                else {
                    costs[i][j] = costs[i + 1][j - 1] + 1;
                }
            }
        }
        vector<vector<int>> dp(n + 1, vector<int>(k + 1, INT_MAX));
        for (int i = 1; i <= n; ++i) {
            for (int j = 1; j <= min(k, i); ++j) {
                if (j == 1) dp[i][j] = costs[0][i - 1];
                else {
                    for (int z = j - 1; z < i; ++z) {
                        dp[i][j] = min(dp[i][j], dp[z][j - 1] + costs[z][i - 1]);
                    }
                }
            }
        }
        return dp[n][k];
    }
};